﻿using System.Collections.Generic;
using UnityEngine;
using Cysharp.Threading.Tasks;
using System;

public abstract class SkillManagerBase: UnitComponentBase
{
    /// <summary>
    /// 技能列表
    /// </summary>
    public List<SkillBase> skills { get; private set; } = new List<SkillBase>();


    // 技能预制体缓存
    private static AssetCache<string, GameObject> s_SkillPrefabCaches = new AssetCache<string, GameObject>(RES_PATH);
    private const string RES_PATH = "SkillEffects/{0}.prefab";


    protected override void OnSpawn()
    {
        for (int i = 0; i < skills.Count; i++)
        {
            skills[i].Reset();
        }
    }

    protected override void OnThaw()
    {
        // TODO
    }

    protected override void OnFreeze()
    {
        // TODO
    }

    /// <summary>
    /// 获取技能效果预制体
    /// </summary>
    public async UniTask<GameObject> GetEffectPrefab(string effectName)
    {
        return await s_SkillPrefabCaches.LoadAsync(effectName);
    }

    /// <summary>
    /// 通过Id查找技能配置
    /// </summary>
    public SkillConfig FindSkillConfig(int id)
    {
        return ConfigLoader.GetConfig<SkillConfig>().Find(c => c.id == id);
    }

    /// <summary>
    /// 添加技能实例
    /// </summary>
    public void AddSkill(SkillConfig config)
    {
        var skillType = Type.GetType($"Skill_{config.id}");
        var skill = Activator.CreateInstance(skillType) as SkillBase;
        skill.SetEntity(entity);
        skill.SetConfig(config);
        skills.Add(skill);
        skill.Init();
    }

    /// <summary>
    /// 添加技能实例
    /// </summary>
    public void AddSkill(int id)
    {
        AddSkill(FindSkillConfig(id));
    }
}